package a.a.a.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import a.a.a.filesystem.ContestRepository;
import a.a.a.filesystem.GroupRepository;

@Controller
public class ContestController {

	@Autowired
	private ContestRepository contestRepository;
	@Autowired
	private GroupRepository groupRepository;

	public ContestRepository getContestRepository() {
		return contestRepository;
	}

	public void setContestRepository(ContestRepository contestRepository) {
		this.contestRepository = contestRepository;
	}

	public GroupRepository getGroupRepository() {
		return groupRepository;
	}

	public void setGroupRepository(GroupRepository groupRepository) {
		this.groupRepository = groupRepository;
	}

	@RequestMapping(value = "groups/{groupId}/contests", method = RequestMethod.POST)
	public String createContest(Model model, HttpServletRequest request,
			@PathVariable String groupId) {
		String contestId = request.getParameter("contestId");
		if (contestId != null) {
			contestId = contestId.trim();
		}
		System.out.println(groupId);
		if (groupRepository.groupExists(groupId)) {
			//<-- BUGGY BUG -->
			if (!contestRepository.contestExists(groupId, contestId)) {
				contestRepository.createContest(groupId, contestId);
				System.out.println(model.asMap().get("groupId"));
				System.out.println(model.asMap().get("allContests"));
				return "pages/group";
			} else {
				model.addAttribute("errorMessage", "Contest already exists");
				return "denial/error";
			}
		}
		model.addAttribute("errorMessage", "Group does not exist");
		return "denial/error";
	}

	@RequestMapping(value = "groups/{groupId}/contests/{contestId}", method = RequestMethod.GET)
	public String navigateContest(Model model, @RequestParam String id) {
		// if exists year
		// and exists contest
		// create new contest in year
		// and navigate to new contest
		// otherwise
		// navigate to error page (contest already exists)
		// otherwise
		// navigate to error page (year does not exist)
		// navigate to folder {id} if exists
		// otherwise navigate to error page
		return null;
	}

	@RequestMapping(value = "groups/{groupId}/contests/{contestId}", method = RequestMethod.DELETE)
	public String deleteContest(Model model, @PathVariable String groupId,
			@PathVariable String contestId) {
		if (contestId != null) {
			contestId = contestId.trim();
		}
		if (groupRepository.groupExists(groupId)) {
			if (contestRepository.contestExists(groupId, contestId)) {
				contestRepository.removeContest(groupId, contestId);
			} else {
				model.addAttribute("errorMessage", "Contest does not exist");
				return "denial/error";
			}
		}
		model.addAttribute("errorMessage", "Group does not exist");
		return "denial/error";
	}
}
